package scrum.client.common;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import scrum.client.Dao;
import scrum.client.admin.User;
import scrum.client.project.Project;
import scrum.client.project.ProjectGroup;
import scrum.client.project.Requirement;

public class RequirementsFilter_ByProjectGroup extends RequirementsFilter {

	public ProjectGroup group;

	public RequirementsFilter_ByProjectGroup() {
		group = null;
	}

	public RequirementsFilter_ByProjectGroup(User user, Project project) {
		group = findGroup(user, project);
	}

	public void setup(User user, Project project) {
		group = findGroup(user, project);
	}

	protected ProjectGroup findGroup(User user, Project project) {
		List<ProjectGroup> pGroups = Dao.get().getProjectGroupsByGroupMember(user);
		ProjectGroup found = null;
		for (int i = 0; i < pGroups.size() && found == null; i++) {
			if (pGroups.get(i).isProject(project)) {
				found = pGroups.get(i);
			}
		}
		return found;
	}

	@Override
	protected List<Requirement> doFilter(List<Requirement> toFilter) {
		Set<Requirement> toRetain = group.getStoriesAssociates();

		if (toRetain != null)
			toFilter.retainAll(toRetain);
		else toFilter = new ArrayList<Requirement>();

		return toFilter;
	}

	@Override
	protected boolean setupDone() {
		return group != null;
	}

}
